unit Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, wsMain, DB, ADODB, wsDB, wsADO, StdCtrls,Menus, ActnList, Buttons,
  ComCtrls,
  wsClasses,wsControls ,fWorkflowEditor,wsRes, ExtCtrls;

type
  TWorkflowUserInterface = class(TCustomWorkflowUserInterface)
  public
    procedure ShowUserTasksDlg(AUserKey: string; AFormMode: TWorkflowFormMode = wfmModal); override;
    procedure ShowUsersTasksDlg(AUserKeys: string; AFormMode: TWorkflowFormMode = wfmModal); override;
    procedure ShowWorkInsTasksDlg(AWorkInsKey: string; AFormMode: TWorkflowFormMode = wfmModal); override;
    procedure ShowWorkflowDefinitionsDlg; override;
    procedure EditWorkflowDefinition(AWorkDef: TWorkflowDefinition); override;
  end;


  TFormMain = class(TForm)
    WorkflowStudio1: TWorkflowStudio;
    WorkflowADODB1: TWorkflowADODB;
    ADOConnection1: TADOConnection;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    lvWorks: TWorkDefListView;
    btAdd: TSpeedButton;
    btEdit: TSpeedButton;
    btDelete: TSpeedButton;
    btRefresh: TSpeedButton;
    btRunNew: TSpeedButton;
    ActionList1: TActionList;
    acImport: TAction;
    acExport: TAction;
    SaveDialog1: TSaveDialog;
    OpenDialog1: TOpenDialog;
    PopupMenu1: TPopupMenu;
    Export1: TMenuItem;
    Import1: TMenuItem;
    Panel1: TPanel;
    cbUser: TComboBox;
    PanelTaskList: TPanel;
    procedure FormCreate(Sender: TObject);
    procedure btAddClick(Sender: TObject);
    procedure btEditClick(Sender: TObject);
    procedure btRefreshClick(Sender: TObject);
    procedure btRunNewClick(Sender: TObject);
    procedure btDeleteClick(Sender: TObject);
    procedure lvWorksEdited(Sender: TObject; Item: TListItem;
      var S: String);
    procedure lvWorksDblClick(Sender: TObject);
    procedure PageControl1Change(Sender: TObject);
    procedure acExportExecute(Sender: TObject);
    procedure Import1Click(Sender: TObject);
    procedure cbUserChange(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormResize(Sender: TObject);
  private
    { Private declarations }
    function SelectedUser: TWorkflowUser;
    procedure ImportFile(AFileName: string);
  public
    { Public declarations }
  end;

var
  FormMain: TFormMain;

implementation
uses TaskList;

{$R *.dfm}

procedure TFormMain.FormCreate(Sender: TObject);
begin
  ADOConnection1.ConnectionString :=
    'Provider=SQLOLEDB.1;Password=manager;Persist Security Info=True;User ID=sa;Initial Catalog=BXXT;Data Source=LIUDSH';


  {Add users and groups}
  with WorkflowStudio1.UserManager do
  begin
    {Add all users}
    Users.Add('1', 'John', 'john@domain');
    Users.Add('2', 'Sarah', 'sarah@domain');
    Users.Add('3', 'Scott', 'scott@domain');
    Users.Add('4', 'Mario', 'mario@domain');
    Users.Add('5', 'Tina', 'tina@domain');

    {Add groups and specify which users belong to each group}
    With Groups.Add('managers') do
    begin
      UserIds.Add('1');
      UserIds.Add('2');
    end;
    With Groups.Add('programmers') do
    begin
      UserIds.Add('3');
      UserIds.Add('4');
      UserIds.Add('5');
    end;
  end;

  {Fill user combo}
  WorkflowStudio1.UserManager.FillUserList(cbUser.Items, true);
  cbUser.ItemIndex := 0;
  cbUserChange(nil);


end;

function TFormMain.SelectedUser: TWorkflowUser;
begin
  result := nil;
  if (cbUser.ItemIndex >= 0) and (cbUser.Items.Objects[cbUser.ItemIndex] is TWorkflowUser) then
    result := TWorkflowUser(cbUser.Items.Objects[cbUser.ItemIndex]);
end;

{ TWorkflowUserInterface }

procedure TWorkflowUserInterface.EditWorkflowDefinition(
  AWorkDef: TWorkflowDefinition);
begin
  inherited;

end;

procedure TWorkflowUserInterface.ShowUsersTasksDlg(AUserKeys: string;
  AFormMode: TWorkflowFormMode);
begin
  inherited;

end;

procedure TWorkflowUserInterface.ShowUserTasksDlg(AUserKey: string;
  AFormMode: TWorkflowFormMode);
begin
  if not Assigned(FormTaskList) then
  begin
    FormTaskList := TFormTaskList.Create(Self, WorkflowStudio);
  //  FormTaskList.WindowState := wsMaximized;

  end;
  try
    FormTaskList.SetFilter(tfUser, AUserKey);
    FormTaskList.LoadData;
    FormTaskList.Show;
  //  FormTaskList.PageControl1.Align := alClient;
    FormTaskList.ManualDock(FormMain.PanelTaskList,nil,alClient);
  finally

  end;
end;

procedure TWorkflowUserInterface.ShowWorkflowDefinitionsDlg;
begin
  with FormMain do
  begin
    PageControl1.ActivePageIndex := 1;
    lvWorks.WorkflowStudio := WorkflowStudio;
    btRefreshClick(nil);
  end;

end;

procedure TWorkflowUserInterface.ShowWorkInsTasksDlg(AWorkInsKey: string;
  AFormMode: TWorkflowFormMode);
begin
  inherited;

end;

procedure TFormMain.FormDestroy(Sender: TObject);
begin
  if Assigned(FormTaskList) then
  begin
    FormTaskList.Free;
    FormTaskList := nil;
  end;
end;

procedure TFormMain.btAddClick(Sender: TObject);
var
  ANewItem: TListItem;
  WorkDef: TWorkflowDefinition;
begin
  ANewItem := lvWorks.AddWorkDefItem;
  WorkDef := lvWorks.WorkDefFromItem(ANewItem);
  WorkflowStudio1.WorkflowManager.SaveWorkflowDefinition(WorkDef);
  lvWorks.SelectItemByWorkDef(WorkDef);
  if lvWorks.Selected <> nil then
    lvWorks.Selected.EditCaption;

end;

procedure TFormMain.btEditClick(Sender: TObject);
begin
  if Assigned(lvWorks.SelectedWorkDef) then
    EditWorkflowDiagram(lvWorks.SelectedWorkDef);
end;

procedure TFormMain.btRefreshClick(Sender: TObject);
begin
  lvWorks.LoadFromDatabase;
end;

procedure TFormMain.btRunNewClick(Sender: TObject);
var
  WorkIns: TWorkflowInstance;
begin
  if lvWorks.SelectedWorkDef <> nil then
  begin
    WorkIns := WorkflowStudio1.WorkflowManager.CreateWorkflowInstance(lvWorks.SelectedWorkDef);

    WorkflowStudio1.WorkflowEngine.RunWorkflow(WorkIns);
  end;

end;

procedure TFormMain.btDeleteClick(Sender: TObject);
begin
  if Assigned(lvWorks.SelectedWorkDef) then
  begin
    if MessageDlg(_str(SConfirmDeleteWorkDef),
      mtConfirmation, [mbYes, mbNo], 0) = mrYes then
    begin
      WorkflowStudio1.WorkflowManager.DeleteWorkflowDefinition(lvWorks.SelectedWorkDef);
      lvWorks.SelectedWorkDef.Free;
    end;
  end;
end;

procedure TFormMain.lvWorksEdited(Sender: TObject; Item: TListItem;
  var S: String);
var
  WorkDef: TWorkflowDefinition;
begin
  WorkDef := lvWorks.WorkDefFromitem(Item);
  if WorkDef <> nil then
  begin
    WorkDef.Name := S;
    WorkflowStudio1.WorkflowManager.SaveWorkflowDefinition(WorkDef);
  end;
end;

procedure TFormMain.lvWorksDblClick(Sender: TObject);
begin
  btEditClick(nil);  
end;

procedure TFormMain.PageControl1Change(Sender: TObject);
begin
  case PageControl1.ActivePageIndex of
    1 : WorkflowStudio1.UserInterface.ShowWorkflowDefinitionsDlg;

  end;

end;

procedure TFormMain.acExportExecute(Sender: TObject);
var
  WorkDef: TWorkflowDefinition;
begin
  SaveDialog1.FileName := lvWorks.SelectedWorkDef.Name + SaveDialog1.DefaultExt;
  if SaveDialog1.Execute then
  begin
    WorkDef := TWorkflowDefinition.Create(WorkflowStudio1);
    try
      WorkflowStudio1.WorkflowManager.LoadWorkflowDefinition(lvWorks.SelectedWorkDef.Key, WorkDef);
      WorkDef.Diagram.SaveToFile(SaveDialog1.FileName);
    finally
      WorkDef.Free;
    end;
  end;

end;

procedure TFormMain.Import1Click(Sender: TObject);
var
  c: integer;
begin
  if OpenDialog1.Execute then
  begin
    for c := 0 to OpenDialog1.Files.Count - 1 do
      ImportFile(OpenDialog1.Files[c]);

    //Refresh list
    lvWorks.LoadFromDatabase;
  end;

end;

procedure TFormMain.ImportFile(AFileName: string);
var
  AWorkDefs: TWorkflowDefinitions;
  WorkName: string;
  i: integer;
  WorkDef: TWorkflowDefinition;
begin
  if FileExists(AFileName) then
  begin
    AWorkDefs := WorkflowStudio1.WorkflowManager.CreateWorkflowDefinitionList;
    try
      {Find an unique name for the workflow definition based on file name}
      WorkName := ChangeFileExt(ExtractFileName(AFileName), '');
      i := 1;
      while AWorkDefs.FindByName(WorkName) <> nil do
      begin
        WorkName := Format('%s (%d)', [ChangeFileExt(ExtractFileName(AFileName), ''), i]);
        inc(i);
      end;

      WorkDef := AWorkDefs.Add;
      WorkDef.Name := WorkName;
      WorkDef.Diagram.LoadFromFile(AFileName);
      WorkflowStudio1.WorkflowManager.SaveWorkflowDefinition(WorkDef);
    finally
      AWorkDefs.Free;
    end;
  end;

end;

procedure TFormMain.cbUserChange(Sender: TObject);
begin
  if SelectedUser <> nil then
  begin
    WorkflowStudio1.UserManager.LoggedUserId := SelectedUser.UserId;
    WorkflowStudio1.UserInterface.ShowUserTasksDlg(SelectedUser.UserId);
  end;
end;

procedure TFormMain.FormResize(Sender: TObject);
begin
  if Assigned(FormTaskList) then
  begin
    FormTaskList.Width := PanelTaskList.Width;
    FormTaskList.Height := PanelTaskList.Height;

  end;

end;

initialization
  UserInterfaceClass := TWorkflowUserInterface;

end.
